/*------------------------------------------------------
                bsp层板载蜂鸣器驱动函数
@brief functions:
.
.
.
.

@attention:

@auther:Cherno
--------------------------------------------------------*/
#include <main.h>
#include "bsp_buzzer.h"
#include "music_notes.h"
#include "bsp_delay.h"
//=====================================================================================================
static uint16_t music[255][3] = {0};
/*-----------------base communication functions
--------------------------------------------------------
@brief:
*/
//
#if RTOS_MODE == 1
#define buzzer_delay(ms) \
    osdelay(ms)
#else
#define buzzer_delay(ms) \
    delay_ms(ms)
#endif
/*----------------- base functions
--------------------------------------------------------
@brief:
*/
//
void buzzer_on(uint16_t psc, uint16_t pwm)
{
    __HAL_TIM_PRESCALER(BUZZER_TIMHANDLE, psc);
    __HAL_TIM_SetCompare(BUZZER_TIMHANDLE, BUZZER_CHANNEL, pwm);
}

//
void buzzer_set(uint32_t freq, uint32_t duty)
{
    uint32_t psc = __HAL_TIM_GET_ICPRESCALER(BUZZER_TIMHANDLE, BUZZER_CHANNEL);

    uint32_t arr = BUZZER_CKPSC / psc / freq - 1;
    uint32_t ccr = (uint32_t)duty * arr / 100;
    __HAL_TIM_SetAutoreload(BUZZER_TIMHANDLE, arr);
    __HAL_TIM_SetCompare(BUZZER_TIMHANDLE, BUZZER_CHANNEL, ccr);
}

void buzzer_off(void)
{
    __HAL_TIM_SetCompare(BUZZER_TIMHANDLE, BUZZER_CHANNEL, 0);
}

static notes_to_music(music_dir_e dir)
{
    
}

//=====================================================================================================
static uint16_t tone_index[8][3] = {
    {0, 0, 0},
    {262, 523, 1046},
    {294, 587, 1175},
    {330, 659, 1318},
    {349, 698, 1397},
    {392, 784, 1568},
    {440, 880, 1760},
    {494, 988, 1976},
};
/*----------------- app functions
--------------------------------------------------------
@brief:
*/
//
void Buzzer_Init(void)
{
    HAL_TIM_PWM_Start(BUZZER_TIMHANDLE, BUZZER_CHANNEL);
}

//
void Buzzer_Test()
{
    uint16_t pwm = 0;
    uint16_t psc = 0;
    pwm++;
    psc++;
    if (pwm > 60000)
    {
        pwm = 30000;
    }
    if (psc > 1000)
    {
        psc = 0;
    }
    buzzer_on(psc, pwm);
    HAL_Delay(1);
}

//
void Music_Select(music_dir_e dir)
{
    switch (dir)
    {
    case super_mario:

        break;
    case lone_brave:
        memcpy(music, Music_Lone_Brave, sizeof(Music_Lone_Brave));
        break;
    default:
        break;
    }

    uint16_t MusicBeatNum = sizeof(music) / sizeof(music[0]) - 1;
    uint16_t MusicSpeed = music[2];
    for (uint16_t i = 1; i <= MusicBeatNum; i++)
    {
        uint8_t note_index = music[i * 3];
        uint8_t note_variant = music[i * 3 + 1];
        uint8_t note_duration = music[i * 3 + 2];
        for (uint16_t i = 1; i <= MusicBeatNum; i++)
        {
            buzzer_set(tone_index[note_index][note_variant], 50);
            buzzer_delay(MusicSpeed / note_duration);
        }
    }
}
/*----------------- API_c
--------------------------------------------------------
@brief:
*/
//

/*------------------------end-------------------------*/